package GUI.Productos;

import java.rmi.RemoteException;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;

import GUI.Administracion.Exceptions.ExcepcionesMenuVentas;
import article.entities.ArticleVO;
import article.entities.IngredientVO;
import article.exceptions.BadArticleException;
import article.exceptions.BadCategoryException;
import article.exceptions.IncompleteArticleException;
import article.exceptions.MissingArticleException;
import article.exceptions.MissingCategoryException;
import article.exceptions.NotHereException;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import dbExceptions.DataBaseErrorException;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.article.interfaces.ArticleProxyMgt;

public class VerProductoMod extends Dialog {

	protected Object result;
	protected Shell shell;
	ArticleVO art;
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public VerProductoMod(Shell parent, int style, ArticleVO articulo) {
		super(parent, style);
		art=articulo;
		setText("Datos del Articulo");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setVisible(true);
		shell.setSize(559, 376);
		shell.setText(getText());
		shell.setLayout(null);
		
		Label lblNombre = new Label(shell, SWT.NONE);
		lblNombre.setBounds(36, 47, 47, 14);
		lblNombre.setText("Nombre");
		
		Label lblPrecio = new Label(shell, SWT.NONE);
		lblPrecio.setBounds(36, 97, 37, 14);
		lblPrecio.setText("Precio");
		
		Label lblCategoria = new Label(shell, SWT.NONE);
		lblCategoria.setBounds(36, 141, 71, 14);
		lblCategoria.setText("Categoria");
		
		Label lblIngredientes = new Label(shell, SWT.NONE);
		lblIngredientes.setBounds(36, 197, 71, 14);
		lblIngredientes.setText("Ingredientes");
		
		Label lblDescripcion = new Label(shell, SWT.NONE);
		lblDescripcion.setBounds(36, 248, 67, 14);
		lblDescripcion.setText("Descripcion");
		
		Label label = new Label(shell, SWT.BORDER | SWT.SEPARATOR | SWT.HORIZONTAL);
		label.setBounds(36, 67, 442, 10);
		label.setVisible(true);
		
		Label label_1 = new Label(shell, SWT.BORDER | SWT.SEPARATOR | SWT.HORIZONTAL);
		label_1.setBounds(36, 117, 442, 14);
		label_1.setVisible(true);
		
		Label label_2 = new Label(shell, SWT.BORDER | SWT.SEPARATOR | SWT.HORIZONTAL);
		label_2.setBounds(36, 161, 442, 30);
		label_2.setVisible(true);
		
		Label label_3 = new Label(shell, SWT.BORDER | SWT.SEPARATOR | SWT.HORIZONTAL);
		label_3.setBounds(46, 218, 432, 10);
		label_3.setVisible(true);
		
		Label label_4 = new Label(shell, SWT.BORDER | SWT.SEPARATOR | SWT.HORIZONTAL);
		label_4.setBounds(36, 268, 442, 25);
		label_4.setVisible(true);
		
		Label nombre = new Label(shell, SWT.NONE);
		nombre.setBounds(151, 47, 67, 14);
		nombre.setText(art.getName());
		
		Label precio = new Label(shell, SWT.NONE);
		precio.setBounds(151, 97, 22, 14);
		precio.setText(String.valueOf(art.getPrice()));
		
		Label categoria = new Label(shell, SWT.NONE);
		categoria.setBounds(151, 141, 67, 14);
		categoria.setText(art.getCat().getCat());
		
		Label ingredientes = new Label(shell, SWT.NONE);
		ingredientes.setBounds(151, 197, 159, 14);
		IngredientVO [] ings = art.getIngr();
		String m="";
		for(int i = 0; i<ings.length;i++){
		m= m.concat(" " + ings[i].getIngr());
		}
		
		ingredientes.setText(m);
		
		
		Label descp = new Label(shell, SWT.NONE);
		descp.setBounds(151, 248, 159, 14);
		descp.setText(art.getDesc());
		
		Button btnModificar = new Button(shell, SWT.NONE);
		btnModificar.setBounds(381, 40, 97, 28);
		btnModificar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				modificarAtributo mod = new modificarAtributo (shell, shell.getStyle(), art, "Nuevo Nombre" , 0);
				shell.setVisible(false);
				mod.open();
			}
		});
		btnModificar.setText("Modificar");
		
		Label label_5 = new Label(shell, SWT.SEPARATOR | SWT.VERTICAL);
		label_5.setBounds(321, 47, 46, 249);
		
		Button btnModificar_1 = new Button(shell, SWT.NONE);
		btnModificar_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				modificarAtributo mod = new modificarAtributo (shell, shell.getStyle(), art, "Nueva Descripcion" ,2);
				shell.setVisible(false);
				mod.open();
			}
		});
		btnModificar_1.setBounds(381, 241, 97, 28);
		btnModificar_1.setText("Modificar");
		
		Button btnModificar_2 = new Button(shell, SWT.NONE);
		btnModificar_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				modificarAtributo mod = new modificarAtributo (shell, shell.getStyle(), art, "Nuevo Precio" , 1);
				shell.setVisible(false);
				mod.open();
			}
		});
		btnModificar_2.setBounds(381, 90, 97, 28);
		btnModificar_2.setText("Modificar");
		
		Button btnModificar_3 = new Button(shell, SWT.NONE);
		btnModificar_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				modAtrib mod = new modAtrib (shell, shell.getStyle(), art);
				shell.setVisible(false);
				try {
					mod.open();
				} catch (RemoteException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (ProxyException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				}
				
			}
		});
		btnModificar_3.setBounds(381, 190, 97, 28);
		btnModificar_3.setText("Modificar");
		
		Button btnNewButton = new Button(shell, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				selectCateg s = new selectCateg (shell, shell.getStyle(), art);
				shell.setVisible(false);
				try {
					s.open();
					shell.dispose();
				} catch (RemoteException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (ProxyException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				}
			}
		});
		btnNewButton.setBounds(381, 141, 97, 28);
		btnNewButton.setText("Modificar");
		
		Button btnAceptar = new Button(shell, SWT.NONE);
		btnAceptar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				ArticleProxyMgt uso = ServiceFacade.getInstance().getArticleProxyMgt();
				try {
					uso.updateArticle(art);
					shell.close();
					
				} catch (RemoteException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (NotHereException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IncompleteArticleException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ProxyException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (MissingCategoryException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (DataBaseErrorException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con la Base de Datos");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (BadCategoryException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (MissingArticleException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Sistema");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (BadArticleException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				}
				
				
			}
		});
		btnAceptar.setBounds(99, 299, 94, 28);
		btnAceptar.setText("Aceptar");
		
		Button btnCancelar = new Button(shell, SWT.NONE);
		btnCancelar.setBounds(354, 299, 94, 28);
		btnCancelar.setText("Cancelar");

	}
}
